<template>
	<div class="padding-across-10" v-loading.fullscreen.lock="pushLoading" element-loading-text="请稍等...">
		<div style="width: 100%">
			<el-row>
				<el-col :span="8">
					<headerOpreate style="width: 100%" :opreate-datas="opreateDatas" v-on:executeAction="executeAction"></headerOpreate>
				</el-col>
				<el-col :span="16">
					<div style="text-align: right">
						<span style="font-size: 13px;font-weight: bold">设备种类</span>
						<el-select style="width: 120px" size="small" v-model="deviceCategory" placeholder="请选择设备类型" @change="selectDeviceCategory">
							<el-option v-for="item in deviceCategoryDatas" :key="item.name" :label="item.value" :value="item.name">
							</el-option>
						</el-select>
						<span style="font-size: 13px;font-weight: bold">检索字段</span>
						<el-select size="small" v-model="value" :placeholder="$t('message.selectModeAlert')" style="width: 90px;margin-right: 2px">
							<el-option label="名称" :value="0"></el-option>
							<el-option label="创建者" :value="1"></el-option>
						</el-select>
						<span style="font-size: 13px;font-weight: bold">关键字</span>
						<el-input style="width: 190px" size="small" v-model="searchModel" placeholder="输入检索字段关键字">
							<el-button id="searchData" slot="append" icon="search" @click="searchFirmware"></el-button>
						</el-input>
					</div>
				</el-col>
			</el-row>
		</div>
		<div class="block" style="text-align:right;float: right;padding-bottom: 2px">
			<el-pagination @current-change="handleCurrentChange" @size-change="handleSizeChange" :current-page="currentPage" :page-size="pageSize" :page-sizes="[10,20,30,40,50]" layout="total,sizes, prev, pager, next, jumper" :total="totalSize">
			</el-pagination>
		</div>
		<el-table border style="width: 100%;" @selection-change="handleSelectionChange" :data="tableData">
			<el-table-column :show-overflow-tooltip="true" align="center" type="selection"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="名称" align="center" prop="name">
				<template slot-scope="scope">
					<a style="color: #50bfff;text-decoration: underline;cursor: pointer" @click="downloadFirmware(scope.row)">{{ scope.row.name }}</a>
				</template>
			</el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="是否存在" align="center">
				<template slot-scope="scope">
					<el-tag type="success" v-if="scope.row.existed">存在</el-tag>
					<el-tag type="danger" v-else>不存在</el-tag>
				</template>
			</el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="文件类型" align="center">
				<template slot-scope="scope">
					<span>固件文件(Firmware)</span>
				</template>
			</el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="适配设备" align="center">
				<template slot-scope="scope">
					<span v-if="scope.row.category == 'WeiTu'">智能微图</span>
					<span v-if="scope.row.category == 'BookCorner'">图书角</span>
					<span v-if="scope.row.category == 'MultimediaIT'">多媒体智能终端</span>
					<span v-if="scope.row.category == 'Self_ServiceMachine'">自助借还书机</span>
					<span v-if="scope.row.category == 'SafetyGate'">安全门</span>
					<span v-if="scope.row.category == 'VideoMonitor'">视频监控</span>
					<span v-if="scope.row.category == 'WirelessGateway'">无线网关</span>
					<span v-if="scope.row.category == 'PC'">电脑</span>
					<span v-if="scope.row.category == 'ZNSJ'">智能书架</span>
				</template>
			</el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="创建者" align="center" prop="uid"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="上传时间" align="center" prop="time" width="200"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="描述" align="center" prop="desc"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="执行操作" align="center">
				<template slot-scope="scope">
					<el-button type="primary" size="small" @click="editAPK(scope.row)"><i class="el-icon-edit el-icon--left"></i>编辑描述</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="block" style="text-align:right;margin-bottom:60px;padding-top: 2px">
			<el-pagination @current-change="handleCurrentChange" @size-change="handleSizeChange" :current-page="currentPage" :page-size="pageSize" :page-sizes="[10,20,30,40,50]" layout="total,sizes, prev, pager, next, jumper" :total="totalSize">
			</el-pagination>
		</div>
		<el-dialog :title="$t('message.insertFirmwareResource')" v-model="dialogUploadMedia" class="upload-section" :class="[dialog-small-width]">
			<div style="width:360px;margin:auto">
				<div style="width: 100%">
					<label>设备类型：</label>
					<el-select size="small" v-model="deviceType" placeholder="请选择设备类型" style="width: 45%;">
						<el-option v-for="item in deviceTypeDatas" :key="item.name" :label="item.value" :value="item.name">
						</el-option>
					</el-select>
				</div>
				<uploadSection style="margin-top: 10px" v-if="dialogUploadMedia" :upload-url="uplodaUrl" :accept-types="acceptTypes" :upload-file-type="uploadFileType" :upload-device-type="deviceType" v-on:uploadComplete="uploadComplete" :usage="'1'"></uploadSection>
			</div>
		</el-dialog>
		<el-dialog title="信息编辑" v-model="editAPKDialog" :class="[dialog-tiny-width]">
			<el-form id="editForm" ref="editForm" label-position="left" label-width="100px" :rules="editRules" :model="editForm">
				<el-form-item label="名称" prop="name">
					<el-input v-model="editForm.name" disabled></el-input>
				</el-form-item>
				<el-form-item label="上传时间" prop="time">
					<el-input v-model="editForm.time" disabled></el-input>
				</el-form-item>
				<el-form-item label="描述" required prop="desc">
					<el-input type="textarea" v-model="editForm.desc"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="editAPKDialog = false">取 消</el-button>
				<el-button type="primary" @click="confirmEditAPK('editForm')">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
	import $ from 'jquery'
	import headerNave from '../../../../../components/headerNave'
	import headerOpreate from '../../../../../components/headerOpreate'
	import { GloabFun, HttpServer } from '../../../../../assets/js/common.js'
	import uploadSection from '../../../../../components/upload'
	let id = 1000;
	export default {
		components: {
			uploadSection,
			headerNave,
			headerOpreate
		},
		data() {
			//回车触发查询按钮
			document.onkeydown = function(event) {
				var e = event || window.event;
				if(e && e.keyCode == 13) { //回车键的键值为13
					$("#searchData").click(); //调用登录按钮的登录事件
				}
			};
			return {
				value: 0,
				searchModel: '',
				isReload: false,
				dialogUploadMedia: false,
				uplodaUrl: '/Api/User/File/Upload',
				acceptTypes: 'bin,ebl',
				uploadFileType: 4,
				playAudioUrl: '',
				playVideoUrl: '',
				playAudioName: '',
				playVideoName: '',
				deviceTypeDatas: [],
				deviceType: '',
				deviceCategoryDatas: [],
				deviceCategory: '',
				pushDevices: [],
				props: {
					label: 'name'
				},
				tableData: [],
				multipleSelection: [],
				currentPage: 1,
				pageSize: 10,
				totalSize: 0,
				editForm: {
					name: '',
					time: '',
					desc: '',
					keyid: null
				},
				editAPKDialog: false,
				editRules: {
					desc: [{
						required: true,
						message: "文件描述不能为空",
						trigger: 'blur'
					}]
				},
				showUpdate: false,
				opreateDatas: [{
					show: true,
					size: "small",
					type: 'primary',
					icon: 'el-icon-eleg-icon_refresh el-icon--left',
					name: '刷新',
					action: 'reloadDatas'
				},
				{
					show: true,
					size: "small",
					type: 'primary',
					icon: 'el-icon-eleg-icon_upload el-icon--left',
					name: '上传',
					action: 'importFirmware'
				},
				{
					show: GloabFun.getUid() == 'admin' ? true : false,
					size: "small",
					type: "danger",
					icon: "el-icon-delete2 el-icon--left",
					name: "删除",
					action: "multipleDeleteMedia"
				}
				]
			}
		},
		props: {
			subArgs: {
				type: Object,
				default: function() {
					return {};
				}
			}
		},
		methods: {
			executeAction(action) {
				if(action) {
					this[action]();
				}
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDatas();
			},
			handleSizeChange(val) {
				this.pageSize = val;
				this.getDatas();
			},
			handleSelectionChange(val) {
				this.multipleSelection = val;
			},
			//编辑
			editAPK(row) {
				this.editForm.name = row.name;
				this.editForm.time = row.time;
				this.editForm.desc = row.desc;
				this.editForm.keyid = row.keyid;
				this.editAPKDialog = true;
			},
			confirmEditAPK(formName) {
				this.$refs[formName].validate((valid) => {
					if(valid) {
						var param = {
							keyid: this.editForm.keyid,
							description: this.editForm.desc,
							UserId: parseInt(GloabFun.getKeyId())
						}
						HttpServer.httpPost.call(this, '/Api/User/File/UpdateCommonFileInfoDes', param, function(response) {}, function(response) {
							if(response.data.code === 0) {
								this.getDatas();
								this.editAPKDialog = false;
								this.$notify({
									title: '成功',
									message: this.$t('message.saveSuccess'),
									type: 'success',
									duration: 2500
								});
							} else {
								this.$notify.error({
									title: '错误',
									message: response.data.msg,
									duration: 2500
								});
							}
						});
					} else {
						return false;
					}
				});
			},
			searchFirmware(){
				this.getDatas();
			},
			selectDeviceCategory() {
				this.getDatas();
			},
			formateSelection() {
				var result = [],
				array = this.multipleSelection;
				for(var i = 0; i < array.length; i++) {
					result.push(array[i].keyid);
				}
				return result;
			},
			//下载
			downloadFirmware(row) {
				if(row.existed == false) {
					this.$notify({
						title: this.$t('message.warning'),
						message: '文件不存在',
						type: 'warning',
						duration: 2500
					});
				} else {
					if(GloabFun.getUid() == 'admin' || GloabFun.getUid() == row.uid) {
						window.location.href = row.url;
					} else {
						this.$notify({
							title: this.$t('message.warning'),
							message: '无操作权限',
							type: 'warning',
							duration: 2500
						});
					}
				}
			},
			//批量删除固件文件
			multipleDeleteMedia() {
				if(this.formateSelection().length > 0) {
					this.$confirm('此操作将删除此APK文件，是否继续？', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						var deleteParam = {
							keyid: this.formateSelection()
						}
						HttpServer.httpPost.call(this, '/Api/User/File/Delete/CommonFile', deleteParam, function(response) {}, function(response) {
							if(response.data.code === 0) {
								this.getDatas();
								this.$notify({
									title: '成功',
									message: this.$t('message.bulkDeleteSuccess'),
									type: 'success',
									duration: 2500
								});
							} else {
								this.getDatas();
								this.$notify.error({
									title: '错误',
									message: response.data.msg,
									duration: 2500
								});
							}
						});
					}).catch(() => {
						this.$notify.info({
							title: '消息',
							message: this.$t('message.cancleOperation'),
							duration: 2500
						})
					})
				} else {
					this.$notify({
						title: this.$t('message.warning'),
						message: this.$t('message.selectBulkDeleteWarning'),
						type: 'warning',
						duration: 2500
					});
				}
			},
			//去掉时间中的T
			formateData(array) {
				for(var i = 0; i < array.length; i++) {
					var index1 = array[i].time.indexOf("T");
					array[i].time = array[i].time.substring(0, index1) + " " + array[i].time.substring(index1 + 1, index1 + 9);
				}
			},
			//查询所有设备类型 -- 用于文件上传
			getAllDeviceType() {
				HttpServer.httpGet.call(this, '/Api/Device/GetAllDeviceCategory?fromwhat=1', function(response) {}, function(response) {
					this.deviceTypeDatas = response.data;
					this.deviceType = response.data[0].name;
				})
			},
			//上传
			importFirmware() {
				this.getAllDeviceType();
				this.dialogUploadMedia = true;
			},
			uploadComplete() {
				this.getDatas();
				this.$notify({
					title: '成功',
					message: this.$t('message.uploadSuccess'),
					type: 'success',
					duration: 2500
				});
			},
			//刷新
			reloadDatas() {
				this.isReload = true;
				this.getDatas();
			},
			//查询所有设备类型 -- 用于页面筛选
			getAllDeviceCategory() {
				HttpServer.httpGet.call(this, '/Api/Device/GetAllDeviceCategory?fromwhat=1', function(response) {}, function(response) {
					response.data.unshift({
						name: '',
						value: '全部'
					});
					this.deviceCategoryDatas = response.data;
					this.deviceCategory = response.data[0].name;
					this.getDatas();
				})
			},
			getDatas() {
				var param = {
					type: 4,
					types: [],
					index: this.currentPage,
					pagesize: this.pageSize,
					createdby: this.value === 1 ? this.searchModel : '',
					name: this.value === 0 ? this.searchModel : '',
					devicetype: this.deviceCategory
				}
				HttpServer.httpPost.call(this, '/Api/User/File/CommonFileInfo', param, function(response) {}, function(response) {
					// console.log('固件资源列表',response);
					if(response.data.code === 0) {
						this.formateData(response.data.Info_);
						this.tableData = response.data.Info_;
						this.totalSize = response.data.totalcount;
						if(this.isReload) {
							this.$notify({
								title: '成功',
								message: this.$t('message.refreshSuccess'),
								type: 'success',
								duration: 2500
							});
						}
					} else {
						this.$notify.error({
							title: '错误',
							message: response.data.msg,
							duration: 2500
						});
					}
					this.isReload = false;
				});
			},
		},
		created() {
			this.getAllDeviceCategory();
		}
	}
</script>
<style scoped>
	@import '../../../../../assets/css/font-awesome-4.7.0/css/font-awesome.css';
</style>
<style scoped lang="less">
	.name {
		color: #20a0ff;
	}
	
	.edit-mission {
		padding-top: 50px;
		.el-dialog__header {
			background-color: #000;
		}
		.el-dialog__body {
			position: relative;
		}
		.clearfloat {
			height: 100px;
			max-height: 150px;
		}
	}
</style>